package com.panda.mall.elastic.controller;

import com.panda.mall.common.result.Result;
import com.panda.mall.elastic.service.ElasticService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * author: tcy
 * createDate: 2023/3/2
 * description: elastic控制层
 */
@Api(tags = "搜索引擎")
@RestController
@RequestMapping("/elastic")
public class ElasticController {

    @Autowired
    ElasticService elasticService;

    @ApiOperation(value = "创建索引", notes = "ES创建索引")
    @GetMapping("/createIndex/{keyword}")
    public Result<String> createIndex(@PathVariable("keyword") String keyword){
        return Result.success(elasticService.createIndex(keyword));
    }

    @ApiOperation(value = "删除索引", notes = "ES删除索引")
    @GetMapping("/deleteIndex/{keyword}")
    public Result<String> deleteIndex(@PathVariable("keyword") String keyword){
        return Result.success(elasticService.deleteIndex(keyword));
    }

    @ApiOperation(value = "查询索引", notes = "ES查询索引")
    @GetMapping("/queryIndex/{keyword}")
    public Result<String> queryIndex(@PathVariable("keyword") String keyword){
        return Result.success(elasticService.queryIndex(keyword));
    }

    @ApiOperation(value = "查询全部索引", notes = "ES查询索引")
    @PostMapping("/listIndex")
    public Result<List<String>> listIndex(){
        return Result.success(elasticService.listIndex());
    }

    @ApiOperation(value = "批量创建文档", notes = "ES批量创建文档")
    @GetMapping("/createBulkDocument/{keyword}/{index}")
    public Result<String> createBulkDocument(@PathVariable("keyword") String keyword, @PathVariable("index") String index){
        return Result.success(elasticService.createDocument(keyword,index));
    }

    @ApiOperation(value = "条件搜索文档", notes = "ES搜索文档")
    @GetMapping("/searchDocument/{keyword}/{index}")
    public Result<List<Map<String,Object>>> searchDocument(@PathVariable("keyword") String keyword, @PathVariable("index") String index){
        return Result.success(elasticService.searchDocument(keyword,index));
    }

    @ApiOperation(value = "创建文档", notes = "创建文档")
    @GetMapping("/addDocument/{id}/{index}")
    public Result<String> addDocument(@PathVariable("id") Long id, @PathVariable("index") String index) {
        return Result.success(elasticService.addDocument(id,index));
    }

    @ApiOperation(value = "查询文档", notes = "查询文档")
    @GetMapping("/queryDocument/{eid}/{index}")
    public Result<Map<String,Object>> queryDocument(@PathVariable("eid") String eid, @PathVariable("index") String index) {
        return Result.success(elasticService.queryDocument(eid,index));
    }

    @ApiOperation(value = "修改文档", notes = "修改文档")
    @GetMapping("/updateDocument/{id}/{eid}/{index}")
    public Result<String> updateDocument(@PathVariable("id") Long id,@PathVariable("eid") String eid, @PathVariable("index") String index) {
        return Result.success(elasticService.updateDocument(id, eid,index));
    }

    @ApiOperation(value = "删除文档", notes = "删除文档")
    @GetMapping("/deleteDocument/{eid}/{index}")
    public Result<String> deleteDocument(@PathVariable("eid") String eid, @PathVariable("index") String index) {
        return Result.success(elasticService.deleteDocument(eid,index));
    }

}
